package com.golxen.framework;


import com.golxen.utils.LogUtil;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

public class PluginSwitch {

	static String secret = "golxen";

	static PluginSwitch instance = null;

	private PluginSwitch() {
		LogUtil.CONSOLE.info("[plugin] start {}", this.getClass().getName());
	}

	public static void start() {
		if (instance != null) {
			return;
		}

		instance = new PluginSwitch();

		for (Class<?> cls : pluginClsList) {
			try {
				Method method = cls.getMethod("start");
				LogUtil.CONSOLE.info("[plugin] start {}", cls.getName());
				method.invoke(null);
			} catch (Exception e) {
				throw new RuntimeException(e);
			}
		}
	}

	public static boolean active() {
		return instance != null;
	}

	static final List<Class<?>> pluginClsList = new ArrayList<>();

	public static void registPlugin(Class<?> cls) {
		if (pluginClsList.contains(cls)) {
			LogUtil.CONSOLE.info("[PluginSwitch] regist plugin repeated {}", cls.getName());
			return;
		}

		pluginClsList.add(cls);
		LogUtil.CONSOLE.info("[PluginSwitch] regist plugin {}", cls.getName());
	}

}
